/**
 * 商品
 */
angular.module('mall.goods.base',[
])

.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider

        //商品列表页
        .state('goods', {
            url : '/shop/:shopid',
            'abstract' : true,
            template: '<ui-view/>',
            resolve:{
                shopinfo: ['$stateParams','ShopService',function($stateParams,ShopService){
                    if ($stateParams.name) {
                        return {
                            shopid: $stateParams.shopid,
                            name: $stateParams.name
                        };
                    }
                    var shopinfo = ShopService.queryShopByShopid($stateParams.shopid);
                    if (shopinfo.$promise) {
                        return shopinfo.$promise;
                    }
                    return shopinfo;
                }],
                menulist: ['$stateParams','ShopMenuService',function($stateParams,ShopMenuService){
                    var menulist = ShopMenuService.getMenuByShopid($stateParams.shopid);
                    return menulist;
                }]
            },
            controller : 'GoodsBaseController'
        })
}])

//商品列表控制器
.controller('GoodsBaseController', ['$scope','$stateParams','ShopMenuService','ShopcartService','shopinfo','menulist',
    function($scope, $stateParams, ShopMenuService,ShopcartService,shopinfo,menulist) {
        $scope.shopid = $stateParams.shopid;
        $scope.shopname = shopinfo.name;
        $scope.goods_list = menulist;

        $scope.goodscontent = ShopcartService.gettotalpriceandcount($scope.shopid);
        $scope.shopbuygoods = ShopcartService.getShopCarByShopid($scope.shopid);

        $scope.add = function(cid,goodsid){//增加按钮
            //ShopMenuService.inputChange($scope.shopid,cid,goodsid);
            ShopcartService.addgoods($scope.shopid,cid,goodsid);
            ShopMenuService.inputChange($scope.shopid,cid,goodsid);
            $scope.goodscontent = ShopcartService.gettotalpriceandcount($scope.shopid);
            $scope.shopbuygoods = ShopcartService.getShopCarByShopid($scope.shopid);
        }

        $scope.remove = function(cid,goodsid){//删除按钮
            ShopcartService.removeGoods($scope.shopid,cid,goodsid);
            $scope.goodscontent = ShopcartService.gettotalpriceandcount($scope.shopid);
            $scope.shopbuygoods = ShopcartService.getShopCarByShopid($scope.shopid);
            if($scope.goodscontent.totalprice==0){
                hide_menu();
            }
            $scope.float_more()
        }

        $scope.cleaning = function(){//清空
            ShopcartService.cleanByShopId($scope.shopid);
            $scope.shopbuygoods = ShopcartService.getShopCarByShopid($scope.shopid);
            $scope.goodscontent = ShopcartService.gettotalpriceandcount($scope.shopid);
            $scope.float_more();
            hide_menu();
        }

        $scope.buy_change=function(cid,goodsid,buycount){
            ShopMenuService.inputChange($scope.shopid,cid,goodsid);
            ShopcartService.importGoods($scope.shopid,cid,goodsid,buycount);
            $scope.goodscontent = ShopcartService.gettotalpriceandcount($scope.shopid);
        }

        $scope.float_show=function(shopid){
            $scope.shopbuygoods=ShopcartService.getShopCarByShopid($scope.shopid);
            if($scope.goodscontent.totalprice>0){
                show_hide();
            }
            $scope.float_more();
        }

        $scope.float_more=function(){
            var height=$("#float_more").height();
            var hei=$("#float_more");
            $scope.good=[];
            for(var i=0;i<$scope.shopbuygoods.length;i++){
                $scope.a=$scope.shopbuygoods[i].goods
                for(var j=0;j<$scope.a.length;j++){
                    $scope.goods=$scope.a[j];
                    $scope.good.push($scope.goods);
                }
            }
            if($scope.good.length>6){
                hei.addClass("float_more");
            }else{
                hei.removeClass("float_more");
            }
        }

        $scope.input = function(cid,goodsid){
            ShopMenuService.inputBlur($scope.shopid,cid,goodsid)
        }
    }
])

;
